package com.qire.manhua.model.viewModel.impl;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.view.View;

import com.qire.common.collect.ListUtil;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.controller.RecommendNewComicController;
import com.qire.manhua.dal.service.RecommendNewComicService;
import com.qire.manhua.databinding.LayoutHomeRecommendNewComicItemBinding;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;
import com.qire.manhua.view.style.RadiusBackgroundSpan;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

/**
 * 首页-推荐页-新作漫画
 */
public class RecommendNewComicViewModel extends SmartRefreshViewModel<IViewModel> {

    private RecommendNewComicService newComicService = new RecommendNewComicService(this);

    private final ArrayList<NewComicItemViewModel> newComicList = new ArrayList<>();
    private final SummerRecyclerBindAdapter<NewComicItemViewModel> newComicAdapter = new SummerRecyclerBindAdapter<>(newComicList);

    private int pageNo = 0;

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(RecommendNewComicController controller) {
        refreshUI();
        newComicAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        newComicAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    /**
     * 重置加载数据
     */
    public void loadNewComicData() {
        resetPage();
        newComicService.loadPageData();
    }

    /**
     * 加载更多数据
     */
    public void moreNewComicData() {
        newComicService.loadPageData();
    }

    /**
     * 重置分页，重新开始刷新数据
     */
    private void resetPage() {
        pageNo = 1;
    }

    /**
     * 翻页，下一页
     */
    public void nextPage() {
        pageNo++;
    }

    /**
     * @return 当前页号
     */
    public int pageNo() {
        return pageNo;
    }

    /**
     * @return true 则表示当前是第一页，否则 false
     */
    public boolean isFirstPage() {
        return pageNo == 1;
    }

    public void clearNewComicList() {
        newComicList.clear();
    }

    public void addNewComicItem(ImageAttribute imgAttr, int comicId, String comicName, String comicAuthor, String comicBrief, String describe, String[] labels) {
        newComicList.add(new NewComicItemViewModel(newComicList.size(), imgAttr, comicId, comicName, comicAuthor, comicBrief, describe, labels));
    }

    public void refreshNewComicAdapter() {
        newComicAdapter.notifyDataSetChanged();
    }

    //endregion #业务数据 end

    // region #UI操作
    public void refreshUI() {
        refreshErrorDataUI();
    }
    public void refreshErrorDataUI() {
        set("errorLayoutVisible", ListUtil.isEmpty(newComicList) ? View.VISIBLE : View.GONE);
    }
    // endregion

    // region #UI属性绑定
    public MutableLiveData<Integer> getErrorLayoutVisible() {
        return get("errorLayoutVisible");
    }

    public LayoutManager getNewComicLayoutManager() {
        return new LinearLayoutManager(null, LinearLayoutManager.VERTICAL, false);
    }

    public Adapter getNewComicAdapter() {
        return newComicAdapter;
    }
    // endregion

    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_new_comic_item)
    public class NewComicItemViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendNewComicItemBinding> {

        public final int serialNumber;
        public final ImageAttribute imgAttr;
        public final int comicId;
        public final String comicName;
        public final String comicAuthor;
        public final String comicBrief;
        public final String describe;
        public final String[] labels;

        public NewComicItemViewModel(int serialNumber, ImageAttribute imgAttr, int comicId, String comicName, String comicAuthor, String comicBrief, String describe, String[] labels) {
            this.serialNumber = serialNumber;
            this.imgAttr = imgAttr;
            this.comicId = comicId;
            this.comicName = comicName;
            this.comicAuthor = comicAuthor;
            this.comicBrief = comicBrief;
            this.describe = describe;
            this.labels = labels;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendNewComicItemBinding viewBind) {
            viewBind.setNewComicItemViewModel(this);
        }

        @Bindable
        public CharSequence getLabel() {

            SpannableStringBuilder builder = new SpannableStringBuilder();

            if(labels!=null) {
                for (String label : labels) {
                    int startIndex = builder.length();
                    builder.append(label + " ");
                    RadiusBackgroundSpan bgSpan = new RadiusBackgroundSpan(Color.rgb(0x99, 0x99, 0x99), 6);//25);
                    builder.setSpan(bgSpan, startIndex, startIndex + label.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                }
            }

            return builder;
        }

    }

}
